<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.member_payment_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot-scope="{row}"
                slot="origin">
        <el-tag v-if="row.origin === 1" type="success">订单来源</el-tag>
        <el-tag v-else-if="row.origin === 2" type="warning">人工操作</el-tag>
        <el-tag v-else type="info">未知</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="amount">
        <el-tag v-if="row.type === 1" type="success">+ {{ row.amount }}</el-tag>
        <el-tag v-else type="danger">- {{ row.amount }}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="status">
        <el-tag v-if="row.status === 1" type="success">完成</el-tag>
        <el-tag v-else type="danger">待支付</el-tag>
      </template>
      <template slot="menuLeft">
        <el-tag type="primary" style="margin-right: 10px">当前余额：￥{{ form.amount }}</el-tag>
        <el-button type="success"
                   size="small"
                   @click="handlePaymentOpen">余额增加/扣除
        </el-button>
      </template>
    </avue-crud>
    <el-dialog title="余额管理"
               append-to-body
               :visible.sync="itemVisible"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               :show-close="false"
               width="30%">
      <el-form :model="paymentForm" ref="form" label-width="80px" class="food-select-form">
        <el-form-item label="类型: " label-width="120px">
          <div>
            <el-select v-model="paymentForm.type" placeholder="请选择增加或扣除">
              <el-option value="1" label="增加">增加</el-option>
              <el-option value="2" label="扣除">扣除</el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="金额: " label-width="120px">
          <div>
            <el-input style="width: 70%" type="number" v-model="paymentForm.amount" placeholder="请输入金额"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="备注: " label-width="120px">
          <div>
            <el-input style="width: 70%" type="textarea" v-model="paymentForm.remark" placeholder="请输入备注"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/member/member_payment";
  import {mapGetters} from "vuex";

  export default {
    name: "MemberPayment",
    props: {
      form: {
        type: Object,
        default: {}
      },
    },
    data() {
      return {
        query: {},
        loading: true,
        page: {
          pageSize: 100,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          menu: false,
          refreshBtn: false,
          index: true,
          viewBtn: true,
          column: [
            {
              label: "金额",
              prop: "amount",
              slot: true,
            },
            {
              label: "来源",
              prop: "origin",
              slot: true,
            },
            {
              label: "订单号",
              prop: "tradeNo",
            },
            {
              label: "备注",
              prop: "remark",
            },
            {
              label: "状态",
              prop: "status",
              slot: true,
            },
            {
              label: "发生时间",
              prop: "createTime",
            },
          ]
        },
        data: [],
        paymentForm: {},
        itemVisible: false,
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn: false
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      handlePaymentOpen() {
        this.itemVisible = true
        this.paymentForm = {
          memberId: this.form.id,
          origin: 2,
        }
      },
      handleClose() {
        this.itemVisible = false
        this.paymentForm = {}
      },
      handleConfirm() {
        if (!this.paymentForm.amount || this.paymentForm.amount === '' || this.paymentForm.amount === 0) {
          this.$message.warning('请输入金额')
          return
        }
        const params = this.paymentForm
        add(params).then(() => {
          this.handleClose()
          this.$emit('reload')
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
        });
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        params.memberId = this.form.id
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style scoped>
.el-select-dropdown .el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
  padding: 0 20px !important;
}
</style>
